//=============================================================================
// CostValue.as
// 
// 
// AUTHOR:            Jay Liang
// 
//                  Copyright (c) 2011 Break Media
//                              All Rights Reserved.
//                          
//=============================================================================
package values.common
{
import com.adobe.serialization.json.JSON;

import mx.utils.StringUtil;

import values.item.IItemValue;
import values.item.INumbericItemValue;
import values.item.ItemTemplateKeyType;
import values.item.ItemTemplateValueList;
import values.item.ItemValue;
import values.level.AvatarLevelValue;

public class CostValue
{		
	private static const XML_PATTERN_BASIC:String = "<item key=\"{0}\" num=\"{1}\" />";
	
	private static const XML_PATTERN:String = "<item key=\"{0}\" num=\"{1}\" params='{2}' />";
	
	private static var playerID:String;
	
	private static var level:AvatarLevelValue;
	
	private static function get currentPlayerLevel():int
	{
		return level.level;
	}
	
	public static function initialize(playerID:String, level:AvatarLevelValue):void
	{
		CostValue.playerID = playerID;
		CostValue.level = level;
	}
	
	private var _xmlData:XMLList;

	public function get xmlData():XMLList
	{
		return _xmlData;
	}
	
	/**
	 * <item key="energy" num="1"/>
	 * <item key="coin" num="10"/> 
	 */	
	[Data(type="field", mapping="item")]
	public function set xmlData(value:XMLList):void
	{
		_xmlData = value;
	}
	
	private var _owner:*;
	
	public function get owner():*
	{
		return _owner;
	}
	
	[Data(type="mapping", way="belong")]
	public function set owner(value:*):void
	{
		_owner = value;
	}
	
	public function get cashCost():Number
	{
		if (xmlData.toXMLString() == "") return 0;
		for each (var obj:* in xmlData)
		{
			if (String(obj.@key) == ItemTemplateKeyType.CASH)
			{
				return obj.@num;
			}
		}
		return -1;
	}
	
	public function get costCount():int
	{
		if (xmlData == null || xmlData.toXMLString() == "") return 0;
		
		return xmlData.length();
	}
    
    public function addItemValueObject(itemKey:String, num:Number, params:* = null):void
    {
		var pstr:String;
		if (params != null && params != "")
		{
			try
			{
				pstr = JSON.encode(params);
			}
			catch (error:Error)
			{
				pstr = params;
			}
		}
		else
		{
			pstr = "";
		}
		
		var str:String;
		if (pstr != "")
		{
			str = StringUtil.substitute(XML_PATTERN, itemKey, num, pstr);
		}
		else
		{
			str = StringUtil.substitute(XML_PATTERN_BASIC, itemKey, num);
		}
		if (!xmlData)
		{
			xmlData = new XMLList(str);
		}
		else
		{
			xmlData = new XMLList(xmlData.toXMLString() + "\n" + str);
		}
    }
	
	public function get costValueList():Vector.<IItemValue>
	{
		var temp:Vector.<IItemValue> = new Vector.<IItemValue>();
		
		if (xmlData == null || xmlData.toXMLString() == "") return temp;
		
		for each (var obj:* in xmlData)
		{
			temp.push(getSingleItemValue(obj));
		}
		return temp;
	}
	
	public static function getSingleItemValue(obj:*):IItemValue
	{
		var params:* = obj.@params;
		if (params != null && params != "")
		{
			try
			{
				params = JSON.decode(params);
			}
			catch (error:Error)
			{
				//do nothing
			}
		}
		else
		{
			params = null;
		}
		var num:int = int(obj.@num);
		var vo:IItemValue = ItemValue.create(ItemTemplateValueList.getByKey(obj.@key), null, params);
		if (vo is INumbericItemValue)
		{
			INumbericItemValue(vo).num = num;
		}
		return vo;
	}
	
	public function clone():CostValue
	{
		var cvo:CostValue = new CostValue();
		cvo.owner = owner;
		cvo.xmlData = xmlData ? xmlData.copy() : null;
		return cvo;
	}
}
}





